package com.wyy.dao.impl;

import com.wyy.bean.Role;
import com.wyy.bean.Users;
import com.wyy.dao.DBUtils;
import com.wyy.dao.UsersDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UsersDaoImpl
 * @Description //TODO
 * @Author wky
 * @Date 2021/9/12 14:51
 * @Version 1.0
 **/
public class UsersDaoImpl extends DBUtils implements UsersDao {
    @Override
    public Users login(String username, String password){
        Users users = null;
        String sql = "select * from users where loginname = ? and password = ?";
        List<String> param = new ArrayList<>();
        param.add(username);
        param.add(password);
        resultSet = query(sql, param);
        if (resultSet == null){
            return null;
        }
        try {
            while (resultSet.next()){
                users = new Users();
                users.setLoginName(resultSet.getString("loginname"));
                users.setPassword(resultSet.getString("password"));
                users.setRealName(resultSet.getString("realname"));
                users.setUserId(resultSet.getInt("userid"));
                users.setRoleId(resultSet.getInt("roleid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return users;
    }
    
    @Override
    public List<Users> getUsersList(int pageIndex, int pageSize) {
        List<Users> usersList = new ArrayList<Users>();;
        try {
            String sql = "select userid,loginname,realname,rolename " +
                    "from users u,role r " +
                    "where u.roleid=r.roleid  " +
                    "limit ?,?";
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql, params);
            while(resultSet.next()){
                //1.取出各表的数据
                Users users = new Users();
                users.setUserId(resultSet.getInt("userid"));
                users.setLoginName(resultSet.getString("loginname"));
                users.setRealName(resultSet.getString("realname"));
        
                Role role = new Role();
                role.setRoleName(resultSet.getString("rolename"));
                //2.建立关系
                users.setRole(role);
                usersList.add(users);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return usersList;
    }
    
    @Override
    public int total() {
        int total = 0;
    
        try {
            String sql = "select count(1) from users u,role r " +
                    "where u.roleid=r.roleid ";
            resultSet = query(sql, null);
            while(resultSet.next()){
                total = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
    
        return total;
    }
    
    @Override
    public int insertUser(Users users) {
        int res = -1;
        try {
            String sql = "insert into users values(null,?,?,?,?,?,?,?,?,?,?)";
            List params = new ArrayList();
            params.add(users.getLoginName());
            params.add(users.getPassword());
            params.add(users.getRealName());
            params.add(users.getSex());
            params.add(users.getEmail());
            params.add(users.getAddress());
            params.add(users.getPhone());
            params.add(users.getCardId());
            params.add(users.getDesc());
            params.add(users.getRoleId());
            res = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
    
        return res;
    }
}
